package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchAttr;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;

    @GetMapping("list.html")
    public String list(SearchParam searchParam, Model model) {

        // ${trademarkList} ${attrsList} ${goodsList} ${pageNo}  ${totalPages } 名称一样!
        // 在远程调用中，底层会将数据转换成linkedhashmap，所以可以直接转为Result<Map>
        Result<Map> search = listFeignClient.search(searchParam);
        // 出于前端数据的考虑这里传递的是map
        model.addAllAttributes(search.getData());

        //  ${urlParam}  ${trademarkParam}  ${propsParamList}  ${orderMap}
        // urlParam 用于存储用户的检索地址
        String urlParam = this.mokeUrlParam(searchParam);

        // 设置品牌面包屑
        String trademarkParam = this.mokeTrademarkParam(searchParam.getTrademark());

        // 设置平台属性面包屑
        List<SearchAttr> propsParamList = this.mokePropsParamList(searchParam.getProps());

        // orderMap 存储排序
        Map<String,Object> orderMap = this.mokeOrderMap(searchParam.getOrder());

        model.addAttribute("orderMap",orderMap);
        model.addAttribute("propsParamList",propsParamList);
        model.addAttribute("trademarkParam",trademarkParam);
        model.addAttribute("urlParam",urlParam);
        model.addAttribute("searchParam",searchParam);

        return "list/index";
    }

    /**
     * 设置排序
     * @param order
     * @return
     */
    private Map<String, Object> mokeOrderMap(String order) {
        Map<String,Object> map = new HashMap<>();

        if (!StringUtils.isEmpty(order)){
            // 分割字符串
            String[] split = order.split(":");
            if (split != null && split.length == 2){
                map.put("type", split[0]);
                map.put("sort", split[1]);
            }
        }else{
            map.put("type", "1");
            map.put("sort", "desc");
        }

        return map;
    }

    /**
     * 设置平台属性面包屑
     * @param props
     * @return
     */
    private List<SearchAttr> mokePropsParamList(String[] props) {
        List<SearchAttr> searchAttrList = new ArrayList<>();
        if (props != null && props.length > 0){
            for (String prop : props) {
                // 分割字符串
                String[] split = prop.split(":");
                SearchAttr searchAttr = new SearchAttr();
                //  根据页面需要的key 编写的！
                searchAttr.setAttrId(Long.parseLong(split[0]));
                searchAttr.setAttrValue(split[1]);
                searchAttr.setAttrName(split[2]);

                searchAttrList.add(searchAttr);
            }
            return searchAttrList;
        }
        return null;
    }

    /**
     * 设置品牌面包屑
     * @param trademark
     * @return
     */
    private String mokeTrademarkParam(String trademark) {

        if (!StringUtils.isEmpty(trademark)){
            // 分割字符串
            String[] split = trademark.split(":");
            if (split != null && split.length == 2){
                return "品牌：" + split[1];
            }
        }

        return null;
    }

    /**
     * 制作urlParam
     * @param searchParam
     * @return
     */
    private String mokeUrlParam(SearchParam searchParam) {

        StringBuffer stringBuffer = new StringBuffer();
        // 判断用户的检索入口
        if (!StringUtils.isEmpty(searchParam.getCategory3Id())){
            stringBuffer.append("category3Id=").append(searchParam.getCategory3Id());
        }

        if (!StringUtils.isEmpty(searchParam.getCategory2Id())){
            stringBuffer.append("category2Id=").append(searchParam.getCategory2Id());
        }

        if (!StringUtils.isEmpty(searchParam.getCategory1Id())){
            stringBuffer.append("category1Id=").append(searchParam.getCategory1Id());
        }
        // 判断是不是关键字进入的检索页面
        if (!StringUtils.isEmpty(searchParam.getKeyword())){
            stringBuffer.append("keyword=").append(searchParam.getKeyword());
        }

        // 判断如果有品牌id 的情况
        if (!StringUtils.isEmpty(searchParam.getTrademark())){
            stringBuffer.append("&trademark=").append(searchParam.getTrademark());
        }

        // 判断有平台属性的问题
        if (searchParam.getProps() != null && searchParam.getProps().length>0){
            String[] props = searchParam.getProps();
            for (String prop : props) {
                stringBuffer.append("&props=").append(prop);
            }
        }

        return "list.html?" + stringBuffer.toString();
    }

}
